import pygame
import time
import random

pygame.init()
screen=pygame.display.set_mode([640,550])
screen.fill([255,255,255])

block_w=50
c_x=block_w//2
c_y=block_w//2
c_r=(block_w-6)//2

N=10
i=0
while i<N:
    j=0
    while j<N:
        pygame.draw.rect(screen,[0,255,0],[i*block_w,j*block_w,block_w,block_w],2)
        j+=1
    i+=1

pygame.draw.circle(screen, [255,0,0], [c_x,c_y], c_r)

b_1=random.randint(0,9)
b_2=random.randint(0,9)
a_1=b_1*50+25
a_2=b_2*50+25
pygame.draw.circle(screen, [0,255,0], [a_1,a_2], c_r)

pygame.display.flip()
def is_out_range(c_x,c_y):
    if c_x<=0 or c_x>=block_w*10:
        return True
    if c_y<=0 or c_y>=block_w*10:
        return True
    return False

running=True
while running:
    for event in pygame.event.get():
        # print(event)
        if event.type == pygame.QUIT:
            running=False
        elif event.type == pygame.KEYDOWN:
            c_x_old=c_x
            c_y_old=c_y
            # print(event.key)
            if event.key == pygame.K_UP:
                print("向上")
                if not is_out_range(c_x,c_y-1*block_w):                            
                    c_y-=block_w
            elif event.key == pygame.K_DOWN:
                print("向下")
                if not is_out_range(c_x,c_y+1*block_w):                            
                    c_y+=1*block_w
            elif event.key == pygame.K_LEFT:
                print("向左")
                if not is_out_range(c_x-1*block_w,c_y):                            
                    c_x-=1*block_w
            elif event.key == pygame.K_RIGHT:
                print("向右")
                if not is_out_range(c_x+1*block_w,c_y):                            
                    c_x+=1*block_w


            if a_1==c_x and a_2==c_y:
                print("你赢了")
                b_1=random.randint(0,9)
                b_2=random.randint(0,9)
                a_z=[]
                for z in range(3,4):
                    a_z=a_1
                    print(a_z)
                a_y=[]
                for y in range(3,4):
                    a_y=a_2
                    print(a_y)
                a_1=b_1*50+25
                a_2=b_2*50+25
                pygame.draw.circle(screen, [0,255,0], [a_1,a_2], c_r)
                pygame.draw.circle(screen,[25,0,0],[a_z,a_y],c_r)
            else:
                pygame.draw.circle(screen, [255,255,255], [c_x_old,c_y_old], c_r)
                pygame.draw.circle(screen, [255,0,0], [c_x,c_y], c_r)
                pygame.display.flip()
pygame.quit()
